package com.spark.usermusicService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.spark.usermusicService.entity.Music;
import com.spark.usermusicService.entity.Playlist;
import com.spark.usermusicService.mapper.JoinMusicMapper;
import com.spark.usermusicService.mapper.PlaylistMapper;
import com.spark.usermusicService.service.PlaylistService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 歌单 服务实现类
 * </p>
 *
 * @author wzk
 * @since 2021-07-01
 */
@Service
public class PlaylistServiceImpl extends ServiceImpl<PlaylistMapper, Playlist> implements PlaylistService {
    @Autowired
    private MusicListServiceImpl musicListService;

    @Override
    public List<Playlist> getPlaylistByUserId(String userId) {
        QueryWrapper<Playlist> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Playlist> list = list(queryWrapper);
        return list;
    }

    @Transactional
    @Override
    public boolean deletePlaylistByUserId(String userId) {
        QueryWrapper<Playlist> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id").eq("user_id", userId);
        List<Playlist> playlists = list(queryWrapper);
        for(Playlist playlist : playlists) {
            musicListService.deleteAllByPlaylistId(playlist.getId());
        }
        UpdateWrapper<Playlist> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        boolean b = remove(updateWrapper);
        return b;
    }

    @Transactional
    @Override
    public boolean deleteById(String playlistId) {
        boolean b = musicListService.deleteAllByPlaylistId(playlistId);
        UpdateWrapper<Playlist> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", playlistId);
        boolean b1 = remove(updateWrapper);
        return b&&b1;
    }

//    @Override
//    public boolean saveOrUpdatePlaylist(Playlist playlist) {
//        return false;
//    }
}
